package com.ruoyi.control.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.dto.Mainventilator1HistoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.Mainventilator1HistoryMapper;
import com.ruoyi.control.domain.Mainventilator1History;
import com.ruoyi.control.service.IMainventilator1HistoryService;

/**
 * 主通风机1号风机历史数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class Mainventilator1HistoryServiceImpl extends ServiceImpl<Mainventilator1HistoryMapper, Mainventilator1History> implements IMainventilator1HistoryService //修改
{
    @Autowired
    private Mainventilator1HistoryMapper mainventilator1HistoryMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询主通风机1号风机历史数据
     *
     * @param id 主通风机1号风机历史数据主键
     * @return 主通风机1号风机历史数据
     */
    @Override
    public Mainventilator1History selectMainventilator1HistoryById(String id) {
        return mainventilator1HistoryMapper.selectMainventilator1HistoryById(id);
    }

    /**
     * 查询主通风机1号风机历史数据列表
     *
     * @param mainventilator1History 主通风机1号风机历史数据
     * @return 主通风机1号风机历史数据
     */
    @Override
    public List<Mainventilator1History> selectMainventilator1HistoryList(Mainventilator1History mainventilator1History) {
        return mainventilator1HistoryMapper.selectMainventilator1HistoryList(mainventilator1History);
    }

    /**
     * 新增主通风机1号风机历史数据
     *
     * @param mainventilator1History 主通风机1号风机历史数据
     * @return 结果
     */
    @Override
    public int insertMainventilator1History(Mainventilator1History mainventilator1History) {
        return mainventilator1HistoryMapper.insertMainventilator1History(mainventilator1History);
    }

    /**
     * 修改主通风机1号风机历史数据
     *
     * @param mainventilator1History 主通风机1号风机历史数据
     * @return 结果
     */
    @Override
    public int updateMainventilator1History(Mainventilator1History mainventilator1History) {
        mainventilator1History.setUpdateTime(DateUtils.getNowDate());
        return mainventilator1HistoryMapper.updateMainventilator1History(mainventilator1History);
    }

    /**
     * 批量删除主通风机1号风机历史数据
     *
     * @param ids 需要删除的主通风机1号风机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteMainventilator1HistoryByIds(String[] ids) {
        return mainventilator1HistoryMapper.deleteMainventilator1HistoryByIds(ids);
    }

    /**
     * 删除主通风机1号风机历史数据信息
     *
     * @param id 主通风机1号风机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteMainventilator1HistoryById(String id) {
        return mainventilator1HistoryMapper.deleteMainventilator1HistoryById(id);
    }

    @Override
    public Integer[] selectMainventilator1HistoryByTagName(String tagName) {
        Integer[] history = new Integer[60 * 24];
        List<Mainventilator1History> historyList = mainventilator1HistoryMapper.selectMainventilator1HistoryByTagName(tagName);
        for (int i = 0; i < 60 * 24; i++) {
            if (i >= historyList.size()) {
                history[i] = 0;
            } else {
                double doubleValue = Double.parseDouble(historyList.get(i).getValue());
                int intValue = (int) doubleValue; // 如果需要四舍五入，可以使用 Math.round(doubleValue)
                history[i] = intValue;
            }
        }
        return history;
    }


    @Override
    public int getMainOneTotal() {
        final String key = "tfOneKey";
        int total = 0;
        String cachedValue = redisCache.getCacheObject(key);

        List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainOneTotal();
        total = list.size();
        redisCache.setCacheObject(key, String.valueOf(total));

//        if (cachedValue == null || cachedValue.isEmpty()) {
//            List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainOneTotal();
//            total = list.size();
//            redisCache.setCacheObject(key, String.valueOf(total));
//        } else {
//            total = Integer.parseInt(cachedValue);
//            System.out.println("读取tfOneKey缓存: " + total);
//        }
        return Integer.parseInt(cachedValue);
    }


    /**
     * 7天通风机故障次数
     * @return
     */
    public List<Mainventilator1HistoryDto> getMainOneTotalFault() {
        final String key = "tfGzKey";
        List<Mainventilator1HistoryDto> cachedValueList = redisCache.getCacheObject(key);

        List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainOneTotalFault();


        if (list == null || list.isEmpty()) {
            //list  为空
            List<Mainventilator1HistoryDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                Mainventilator1HistoryDto abnormalCountDto = new Mainventilator1HistoryDto();
                abnormalCountDto.setCount("0");
                defaultList.add(abnormalCountDto);
            }
            List<String> collect = defaultList.stream().map(objInList -> {
                String abnormalCount = objInList.getCount();
                return abnormalCount;
            }).collect(Collectors.toList());
            redisCache.setCacheObject(key, collect);
        } else {
            List<Mainventilator1HistoryDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                Mainventilator1HistoryDto abnormalCountDto = new Mainventilator1HistoryDto();
                abnormalCountDto.setCount("0");
                if (abnormalCountDto.getUpdateDate() != null) {
                    abnormalCountDto.setUpdateDate(abnormalCountDto.getUpdateDate());
                }
                resultList.add(abnormalCountDto);
            }

            redisCache.setCacheObject(key, resultList);

        }

        return cachedValueList;
    }

    /**
     * 昨日功耗
     * @param s
     */
    public List<Mainventilator1HistoryDto> getMainventilatorPowerValueList(String str) {

        String[] keys = {"tfGhKey1", "tfGhKey2", "tfGhKey3", "tfGhKey4"};
        String[] prefixes = {"1#", "2#", "3#", "4#"};

        if (!"1".equals(str) && !"2".equals(str) && !"3".equals(str) && !"4".equals(str)) {
            return null;
        }

        int index = Integer.parseInt(str) - 1;
        String key = keys[index];
        String dy = prefixes[index] + "变频器输入电压";
        String dl = prefixes[index] + "变频器输入电流";

        List<Mainventilator1HistoryDto> cachedValueList = redisCache.getCacheObject(key);
        if (cachedValueList != null) {
            return cachedValueList;
        }

        List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainventilatorPowerValueList(dy, dl);
        List<Mainventilator1HistoryDto> processedList = list.stream().map(objInList -> {
            Double avgPower = objInList.getAvgPower();
            Double consumptionValue = avgPower * 60 * 60;
            objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", consumptionValue)));
            return objInList;
        }).collect(Collectors.toList());

        redisCache.setCacheObject(key, processedList);
        return processedList;


//        final String key = "tfGhKey1";
//        final String key2 = "tfGhKey2";
//        final String key3 = "tfGhKey3";
//        final String key4 = "tfGhKey4";
//        List<Mainventilator1HistoryDto> cachedValueList = null;
//        if ("1".equals(str)){
//            cachedValueList = redisCache.getCacheObject(key);
//            String dy = "1#变频器输入电压";
//            String dl = "1#变频器输入电流";
//            List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainventilatorPowerValueList(dy,dl);
//
//            List<Mainventilator1HistoryDto> collect = list.stream().map(objInList -> {
//                Double avgPower = objInList.getAvgPower();
//                Double value = avgPower * 60 * 60;
//                objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", value)));
//                return objInList;
//            }).collect(Collectors.toList());
//
//            redisCache.setCacheObject(key,collect);
//            return cachedValueList;
//        }
//
//        if ("2".equals(str)){
//            cachedValueList = redisCache.getCacheObject(key2);
//            String dy = "2#变频器输入电压";
//            String dl = "2#变频器输入电流";
//            List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainventilatorPowerValueList(dy,dl);
//
//            List<Mainventilator1HistoryDto> collect = list.stream().map(objInList -> {
//                Double avgPower = objInList.getAvgPower();
//                Double value = avgPower * 60 * 60;
//                objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", value)));
//                return objInList;
//            }).collect(Collectors.toList());
//
//            redisCache.setCacheObject(key2,collect);
//
//            return cachedValueList;
//        }
//
//        if ("3".equals(str)){
//            cachedValueList = redisCache.getCacheObject(key3);
//            String dy = "3#变频器输入电压";
//            String dl = "3#变频器输入电流";
//            List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainventilatorPowerValueList(dy,dl);
//            List<Mainventilator1HistoryDto> collect = list.stream().map(objInList -> {
//                Double avgPower = objInList.getAvgPower();
//                Double value = avgPower * 60 * 60;
//                objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", value)));
//                return objInList;
//            }).collect(Collectors.toList());
//
//            redisCache.setCacheObject(key3,collect);
//
//            return cachedValueList;
//        }
//
//        if ("4".equals(str)){
//            cachedValueList = redisCache.getCacheObject(key4);
//            String dy = "4#变频器输入电压";
//            String dl = "4#变频器输入电流";
//            List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainventilatorPowerValueList(dy,dl);
//
//            List<Mainventilator1HistoryDto> collect = list.stream().map(objInList -> {
//                Double avgPower = objInList.getAvgPower();
//                Double value = avgPower * 60 * 60;
//                objInList.setConsumptionValue(Double.valueOf(String.format("%.2f", value)));
//                return objInList;
//            }).collect(Collectors.toList());
//
//            redisCache.setCacheObject(key4,collect);
//
//            return cachedValueList;
//        }
//        return cachedValueList;
    }
}
